package com.example.transfer.controller;

import com.example.serviceA.api.ServiceAAccountApi;
import com.example.serviceA.req.TransferOutReq;
import com.example.serviceB.api.ServiceBAccountApi;
import com.example.serviceB.req.TransferInReq;
import com.example.transfer.service.ITransferService;
import org.bytesoft.compensable.Compensable;
import org.bytesoft.compensable.CompensableCancel;
import org.bytesoft.compensable.CompensableConfirm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@Compensable(interfaceClass = ITransferService.class, simplified = true)
@RestController
public class TransferController implements ITransferService {

    @Autowired
    private ServiceAAccountApi serviceAAccountApi;

    @Autowired
    private ServiceBAccountApi serviceBAccountApi;

    @Transactional
    @GetMapping("/transfer")
    public void transfer() {
        TransferOutReq transferOutReq = new TransferOutReq();
        transferOutReq.setAccountId(1L);
        transferOutReq.setAmount(BigDecimal.valueOf(100L));
        serviceAAccountApi.transferOut(transferOutReq);

        TransferInReq transferInReq = new TransferInReq();
        transferInReq.setAccountId(2L);
        transferInReq.setAmount(BigDecimal.valueOf(100L));
        serviceBAccountApi.transferIn(transferInReq);
    }

    @CompensableConfirm
    @Transactional
    public void confirmTransfer() {
        System.out.println("==================confirmTransfer===================");
    }

    @CompensableCancel
    @Transactional
    public void cancelTransfer() {
        System.out.println("==================cancelTransfer===================");
    }

}
